var data = require('./js/commodityDB');
var user = require('./js/UserDB');
var wy_gjqs = require('./js/gjqsDB');
var wy_bzzx = require('./js/bzzxDB');
var wy_ppzc = require('./js/ppzcDB');
var wy_spsx = require('./js/spsxDB');
const express = require('express');

const app = express();

const bodyParser = require('body-parser');
const { json } = require('body-parser');
const { request, response } = require('express');
app.use(bodyParser.urlencoded({ extended: false }));




/* 初始化商品 */

app.all("/getCommdity", (request, response) => {

    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');

    response.send(data.ary);
});

/* 初始化推荐 */
app.all("/get", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');

    response.send(data.aryTwo);
});


/* 校验手机号是否存在 */
app.all("/queryNumber", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    let bool;
    let name = request.query.name;
    if (user.ary.length != 0) {
        for (let i = 0; i < user.ary.length; i++) {
            if (user.ary[i].admin == name) {
                bool = false;
            } else {
                bool = true;
            }
        }
    } else {
        bool = true;
    }
    response.send(bool);

})

/* 注册账号 */
app.all("/addUser", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    let admin = request.query.name;
    let pwd = request.query.pwd;
    let u = new user.User(admin, pwd);
    user.ary.push(u);
    response.send(true);
})

/* 登陆校验 */
app.all("/login", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    let admin = request.query.name;
    let pwd = request.query.pwd;
    let u = new user.User(admin, pwd);
    let ren;
    for (let i = 0; i < user.ary.length; i++) {

        if (u.admin == user.ary[i].admin) {

            if (u.password == user.ary[i].password) {
                ren = user.ary[i];
            }
        }
    }
    if (ren != undefined || ren != null) {
        user.log.push(ren);
        response.send(JSON.stringify(ren));
    } else {
        response.send(false);
    }
})



app.all("/gjqs", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    response.send(wy_gjqs.data);
});
app.all("/bzzx", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    response.send(wy_bzzx.data);
});
app.all("/ppzc", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    response.send(wy_ppzc.data);
});
app.all("/spsx", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    response.send(wy_spsx.data);
});
app.all("/comf", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');

    var a = new user.site(request.query.name, "", request.query.DA, request.query.phone);


    for (let i = 0; i < user.log[0].orderForm.length; i++) {
        user.log[0].orderForm[i].site = a;
    }

    response.send(JSON.stringify(a));
});


/* 找到登陆用户 */
app.all("/goShopping", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    if (user.log.length != 0) {
        for (let i = 0; i < user.log.length; i++) {
            response.send(user.log[i]);
        }
    } else {
        response.send(false);
    }

})

//////
//初始页面用的商品数据
var initGoodsDB = require('./data/goodsDB').goodsDB;


//用户添加商品到购物车中的数据
var goodsInCarDB = [];
//打印在终端窗口加入购物车的的商品图片
var output_imgInCar = "";


/* 添加购物车  首页初始化商品 */
var i = 1;
app.get("/jumeiyoupin/homepage", (request, response) => {
    //允许该请求跨域
    response.setHeader("Access-Control-Allow-Origin", "*");
    //将加入购物车的商品添加到服务器上
    var shoping;

    if (request.query.add_shoppingcar) {

        for (var i = 0; i < initGoodsDB.length; i++) {
            if (request.query.add_shoppingcar == initGoodsDB[i].img) {
                shoping = new user.Shopping(initGoodsDB[i].img,
                    initGoodsDB[i].introduction, 0, Number(initGoodsDB[i].price), 0, 1, initGoodsDB[i].price * this.num);
                break;
            }
        }
        if (user.log[0].shopping.length != 0) {

            let us = null;
            console.log(user.log[0].shopping.length);
            var flag = false;
            for (let j = 0; j < user.log[0].shopping.length; j++) {
                console.log(user.log[0].shopping[j].name + "------" + shoping.name);
                if (user.log[0].shopping[j].name == shoping.name) {
                    flag = true;
                    us = user.log[0].shopping[j];
                    /* 如果有 数量 和金额添加 */
                    break;
                } else {
                    /* 没有则添加 */
                }
            }

            if (!flag) {

                user.log[0].shopping.push(shoping);
            } else {
                if (us != null || us != undefined) {

                    us.num += 1;
                    us.allPrices = us.num * us.money;
                }
            }
        } else {
            /* 里面没数据直接加 */

            user.log[0].shopping.push(shoping);
        }

    }

    //将数据响应给homepage.html
    if (request.query.inite_homepage) {
        response.send(initGoodsDB);
    }

    //将数据响应给shoppingcar.html
    if (request.query.add_shoppingcar) {
        response.send(goodsInCarDB);
    }
})


app.all("/gouwuche", (request, response) => {
        response.setHeader('Access-Control-Allow-Origin', '*');
        response.setHeader('Access-Control-Allow-Headers', '*');
        if (user.log[0] != null) {
            response.send(user.log[0].shopping);
        } else {
            response.send(null);
        }
    })
    /* 退出删登陆用户 */
app.all("/Exit", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    user.log[0] = null;
    response.send(true);
})

/* 删除购物车 */
app.all("/deleteShopping", (request, response) => {
        response.setHeader('Access-Control-Allow-Origin', '*');
        response.setHeader('Access-Control-Allow-Headers', '*');
        let name = request.query.name;
        name = name.trim();
        let make = -1;
        for (let i = 0; i < user.log[0].shopping.length; i++) {


            if (user.log[0].shopping[i].name == name) {
                make = i;
                break;
            }
        }
        console.log(make);
        if (make != -1) {
            user.log[0].shopping.splice(make, 1);
        }
        response.send(true);

    })
    /* 增加或者减少数量 */
app.all("/increase", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    let bool = request.query.bool;
    let name = request.query.name;
    name = name.trim();
    let num = request.query.num;
    let allPrices = request.query.allPrices;
    console.log(bool);
    let make = -1;
    if (bool) {
        console.log("进来了");
        for (let i = 0; i < user.log[0].shopping.length; i++) {
            if (user.log[0].shopping[i].name == name) {
                make = i;
                break;
            }
        }
        if (make != -1) {

            user.log[0].shopping[make].num = num;
            user.log[0].shopping[make].allPrices = allPrices;
            console.log(user.log[0].shopping[make]);
        }
    }
    response.send(true);
})

/* 生成订单 */
app.all("/addOrder", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    for (let i = 0; i < user.log[0].shopping.length; i++) {
        let orderForm = new user.orderForm(
            user.log[0].shopping[i].img,
            user.log[0].shopping[i].name,
            user.log[0].shopping[i].num,
            user.log[0].shopping[i].allPrices,
            user.log[0].shopping[i].money
        );
        user.log[0].orderForm.push(orderForm);
    }

    console.log(user.log[0].orderForm);
    response.send(true);
})

/* 初始化订单数据 */
app.all("/order", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    if (user.log[0].orderForm.length != 0) {
        response.send(user.log[0].orderForm);
    } else {
        response.send(null);
    }

})

/* 提交订单并删除购物车数据 */
app.all("/submit", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    for (let i = 0; i < user.log[0].orderForm.length; i++) {
        if (user.log[0].orderForm[i].state == "未付款") {
            user.log[0].orderForm[i].state = "已付款,未发货"
        }
    }
    user.log[0].shopping = new Array();
    response.send(true);
})


app.all("/money_site", (request, response) => {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    let money = 0;
    let userName = "";

    let detailed = ""; //详细地址
    let number = ""; //手机号
    for (let i = 0; i < user.log[0].orderForm.length; i++) {
        money = Number(money) + Number(user.log[0].orderForm[i].price);
        userName = user.log[0].orderForm[i].site.UserName;
        detailed = user.log[0].orderForm[i].site.detailed;
        number = user.log[0].orderForm[i].site.number;
    }
    let obj = {
        price: money,
        userName: userName,
        detailed: detailed,
        number: number
    }
    console.log(obj);
    response.send(JSON.stringify(obj))
})



const port = 8400;
app.listen(8400, () => {
    console.log("web应用服务器启动成功,端口号：" + port);
})